package com.gbiac.toc.api.eventapi.dao;

import com.gbiac.toc.api.eventapi.vo.DepEmployeeVo;
import com.gbiac.toc.api.eventapi.vo.Event;
import com.gbiac.toc.api.eventapi.vo.EventAreaVo;
import com.gbiac.toc.api.eventapi.vo.EventComment;
import com.gbiac.toc.api.eventapi.vo.EventDynamics;
import com.gbiac.toc.api.eventapi.vo.EventInfoVo;
import com.gbiac.toc.api.eventapi.vo.EventType;
import com.gbiac.toc.api.eventapi.vo.EventTypeVo;
import com.gbiac.toc.api.eventapi.vo.EventVo;
import com.gbiac.toc.api.eventapi.vo.TocIndex;
import com.gbiac.toc.comment.entity.EventCommentEntity;
import com.gbiac.toc.dynamics.entity.EventDynamicsEntity;
import com.gbiac.toc.event.entity.EventEntity;
import java.math.BigInteger;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.jeecgframework.core.common.dao.impl.CommonDao;
import org.jeecgframework.core.util.DateUtils;
import org.jeecgframework.core.util.UUIDGenerator;
import org.springframework.stereotype.Repository;

@Repository
public class EventApiDao
  extends CommonDao
{
  public Map<String, Object> getEventCount(String userId)
  {
    Map<String, Object> map = new HashMap();
    Session session = getSession();
    
    String sql = "select count(*) from t_s_base_user u,toc_event e WHERE u.id=e.recipient and u.id='" + userId + "' and e.isdelete=0";
    SQLQuery query = session.createSQLQuery(sql);
    int myEventNum = Integer.parseInt(query.list().get(0).toString());
    
    String sql1 = "select count(*) from toc_event t WHERE t.status in (1,3,5) and t.isdelete=0";
    SQLQuery query1 = session.createSQLQuery(sql1);
    int waitingEventNum = Integer.parseInt(query1.list().get(0).toString());
    
    String depIdsql = "select org_id from t_s_user_org where user_id = '" + userId + "'";
    SQLQuery dquery = session.createSQLQuery(depIdsql);
    List list2 = dquery.list();
    String depId = "";
    if ((list2 != null) && (list2.size() > 0)) {
      depId = list2.get(0).toString();
    }
    String sql2 = "select count(*) from toc_event e WHERE e.depart='" + depId + "' and e.isdelete=0";
    SQLQuery query2 = session.createSQLQuery(sql2);
    int departmentEventNum = Integer.parseInt(query2.list().get(0).toString());
    
    String sql3 = "select count(*) from toc_event e where e.isdelete=0";
    SQLQuery query3 = session.createSQLQuery(sql3);
    int allEventNum = Integer.parseInt(query3.list().get(0).toString());
    
    String sql4 = "select count(*) from toc_follow e where e.userid='" + userId + "'";
    SQLQuery query4 = session.createSQLQuery(sql4);
    int importEventNum = Integer.parseInt(query4.list().get(0).toString());
    
    String sql5 = "select count(*) from toc_event e where e.depart= '0' and e.isdelete=0";
    SQLQuery query5 = session.createSQLQuery(sql5);
    int irresponsibleEventNum = Integer.parseInt(query5.list().get(0).toString());
    
    String sql6 = "select count(*) from toc_daily_work";
    SQLQuery query6 = session.createSQLQuery(sql6);
    int dailyWorkEventNum = Integer.parseInt(query6.list().get(0).toString());
    
    map.put("waitingEventNum", Integer.valueOf(waitingEventNum));
    map.put("myEventNum", Integer.valueOf(myEventNum));
    map.put("allEventNum", Integer.valueOf(allEventNum));
    map.put("departmentEventNum", Integer.valueOf(departmentEventNum));
    map.put("importantEventNum", Integer.valueOf(importEventNum));
    map.put("irresponsibleEventNum", Integer.valueOf(irresponsibleEventNum));
    map.put("projectEventNum", Integer.valueOf(dailyWorkEventNum));
    
    return map;
  }
  
  public EventVo getMyEventInfo(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo myVo = new EventVo();
    Session session = getSession();
    String sql = "";
    if ("1".equals(eventStatus))
    {
      String sqlcount1 = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=3 and e.isdelete=0 AND e.status in(4,8) AND u.id='" + userId + "') t";
      
      String sqlcount2 = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=2 AND e.status in(4,8) AND e.isdelete=0 AND u.id='" + userId + "') t";
      
      String sqlcount3 = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=1 AND e.status in(4,8) AND e.isdelete=0 AND u.id='" + userId + "') t";
      
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      SQLQuery queryCount2 = session.createSQLQuery(sqlcount2);
      SQLQuery queryCount3 = session.createSQLQuery(sqlcount3);
      int outEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
      int nearlyOutEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
      int dealingEventNum = Integer.parseInt(queryCount3.list().get(0).toString());
      myVo.setEventTimeoutTotal(outEventNum);
      myVo.setEventDealingTotal(dealingEventNum);
      myVo.setEventOutingTotal(nearlyOutEventNum);
      if ("1".equals(subStatus)) {
        sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=3 AND e.status in(4,8) AND e.isdelete=0 AND u.id='" + userId + "' order by e.isrecive desc,e.create_date DESC limit ?,?) t";
      } else if ("2".equals(subStatus)) {
        sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=2 AND e.status in(4,8) AND e.isdelete=0 AND u.id='" + userId + "' order by e.isrecive desc,e.create_date DESC limit ?,?) t";
      } else if ("3".equals(subStatus)) {
        sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.timeout_status=1 AND e.status in(4,8) AND e.isdelete=0 AND u.id='" + userId + "' order by e.isrecive desc,e.create_date DESC limit ?,?) t";
      }
    }
    else if ("2".equals(eventStatus))
    {
      String totalSql = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.status in(5,6) AND e.isdelete=0 AND u.id='" + userId + "') c";
      
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int solvedNum = Integer.parseInt(totalCount.list().get(0).toString());
      myVo.setEventSolvedTotal(solvedNum);
      
      sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.status in(5,6) AND e.isdelete=0 AND u.id='" + userId + "' order by create_date DESC limit ?,?) t";
    }
    else if ("3".equals(eventStatus))
    {
      String totalSql = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.status=7 AND e.isdelete=0 AND u.id='" + userId + "') c";
      
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int closedNum = Integer.parseInt(totalCount.list().get(0).toString());
      myVo.setEventSolvedTotal(closedNum);
      sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.status=7 AND e.isdelete=0 AND u.id='" + userId + "' order by create_date DESC limit ?,?) t";
    }
    else if ("4".equals(eventStatus))
    {
      String sqlcount1 = "SELECT count(*) FROM ( SELECT e.id,e.title,e.event_type,e.create_date FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.isdelete=0 AND u.id='" + userId + "') t";
      
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      int total = Integer.parseInt(queryCount1.list().get(0).toString());
      myVo.setEventTotal(total);
      sql = "SELECT t.id,t.title,t.create_date,t.event_type,t.content,t.status FROM ( SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM t_s_base_user u,toc_event e WHERE u.id=e.recipient AND e.isdelete=0 AND u.id='" + userId + "' order by e.isrecive desc,e.create_date DESC limit ?,?) t";
    }
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventCreateDate(obj[2] + "");
        if (obj[3] != null) {
          event.setEventType(getEventTypeName(obj[3] + ""));
        }
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        
        String eventId = obj[0] + "";
        boolean flag = getFocusEventStatus(eventId, userId);
        if (flag) {
          event.setFollow("1");
        } else {
          event.setFollow("0");
        }
        eventList.add(event);
      }
    }
    myVo.setEventList(eventList);
    return myVo;
  }
  
  public EventVo getApplyingEventList(String userId, String departId, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    
    String sqlCount = "SELECT COUNT(*) FROM toc_event e WHERE  e.status=1 and e.isdelete=0 and e.userid in (SELECT t.user_id FROM t_s_user_org  t where t.org_id = '" + departId + "')";
    
    String sqlCount1 = "SELECT COUNT(*) FROM toc_event e WHERE e.status=3 and e.isdelete=0 and e.depart = '" + departId + "'";
    
    String sqlCount2 = "SELECT COUNT(*) FROM toc_event e WHERE e.status=5 and e.isdelete=0 and e.depart = '" + departId + "'";
    SQLQuery queryCount1 = session.createSQLQuery(sqlCount);
    SQLQuery queryCount2 = session.createSQLQuery(sqlCount1);
    SQLQuery queryCount3 = session.createSQLQuery(sqlCount2);
    int eventSubmitTotal = Integer.parseInt(queryCount1.list().get(0).toString());
    int eventSendTotal = Integer.parseInt(queryCount2.list().get(0).toString());
    int eventAcceptTotal = Integer.parseInt(queryCount3.list().get(0).toString());
    vo.setEventSubmitTotal(eventSubmitTotal);
    vo.setEventSendTotal(eventSendTotal);
    vo.setEventAcceptTotal(eventAcceptTotal);
    
    String sql = "";
    if ("1".equals(subStatus)) {
      sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM toc_event e WHERE  e.isdelete=0 and  e.userid in (SELECT t.user_id FROM t_s_user_org  t where t.org_id = '" + departId + "')  and e.status='" + subStatus + "' order by e.create_date DESC limit ?,?";
    } else if (("3".equals(subStatus)) || ("5".equals(subStatus))) {
      sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM toc_event e WHERE e.isdelete=0 and  e.depart='" + departId + "'and e.status='" + subStatus + "' order by e.create_date DESC limit ?,?";
    }
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventType(getEventTypeName(obj[2] + ""));
        event.setEventCreateDate(obj[3] + "");
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        
        String eventId = obj[0] + "";
        boolean flag = getFocusEventStatus(eventId, userId);
        if (flag) {
          event.setFollow("1");
        } else {
          event.setFollow("0");
        }
        eventList.add(event);
      }
    }
    vo.setEventList(eventList);
    return vo;
  }
  
  public EventVo getDepartEventList(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    String depIdsql = "select org_id from t_s_user_org where user_id = '" + userId + "'";
    SQLQuery dquery = session.createSQLQuery(depIdsql);
    List list2 = dquery.list();
    String depId = "";
    if ((list2 != null) && (list2.size() > 0)) {
      depId = list2.get(0).toString();
    }
    String sql = "";
    if (StringUtils.isNotBlank(depId))
    {
      if ("1".equals(eventStatus))
      {
        String sqlcount1 = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8)  and e.timeout_status=3 and e.isdelete=0";
        String sqlcount2 = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8)  and e.timeout_status=2 and e.isdelete=0";
        String sqlcount3 = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8)  and e.timeout_status=1 and e.isdelete=0";
        SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
        SQLQuery queryCount2 = session.createSQLQuery(sqlcount2);
        SQLQuery queryCount3 = session.createSQLQuery(sqlcount3);
        int outEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
        int nearlyOutEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
        int dealingEventNum = Integer.parseInt(queryCount3.list().get(0).toString());
        vo.setEventTimeoutTotal(outEventNum);
        vo.setEventDealingTotal(dealingEventNum);
        vo.setEventOutingTotal(nearlyOutEventNum);
        if ("1".equals(subStatus)) {
          sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8) and e.isdelete=0 " + " and e.timeout_status=3  order by e.isrecive desc,e.create_date DESC limit ?,?";
        } else if ("2".equals(subStatus)) {
          sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8) and e.isdelete=0 " + " and e.timeout_status=2 order by e.isrecive desc,e.create_date DESC limit ?,?";
        } else if ("3".equals(subStatus)) {
          sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' and e.status in(4,8) and e.isdelete=0 " + " and e.timeout_status=1 order by e.isrecive desc,e.create_date DESC limit ?,?";
        }
      }
      else if ("2".equals(eventStatus))
      {
        String totalSql = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.status in(5,6) and e.isdelete=0 order by e.create_date DESC";
        SQLQuery totalCount = session.createSQLQuery(totalSql);
        int solvedNum = Integer.parseInt(totalCount.list().get(0).toString());
        vo.setEventSolvedTotal(solvedNum);
        
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' and e.status in(5,6) and e.isdelete=0 order by e.create_date DESC limit ?,?";
      }
      else if ("3".equals(eventStatus))
      {
        String totalSql = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.status =7 and e.isdelete=0 order by e.create_date DESC";
        SQLQuery totalCount = session.createSQLQuery(totalSql);
        int closedNum = Integer.parseInt(totalCount.list().get(0).toString());
        vo.setEventSolvedTotal(closedNum);
        
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' and e.status =7 and e.isdelete=0 order by e.create_date DESC limit ?,?";
      }
      else if ("4".equals(eventStatus))
      {
        String sqlcount1 = "select count(*) from toc_event e WHERE  e.depart='" + depId + "' and e.isdelete=0 ";
        SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
        int total = Integer.parseInt(queryCount1.list().get(0).toString());
        vo.setEventTotal(total);
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.depart='" + depId + "' " + " and e.isdelete=0 order by e.isrecive desc,e.create_date DESC limit ?,?";
      }
      SQLQuery query = session.createSQLQuery(sql);
      int curpage = Integer.parseInt(page);
      int size = Integer.parseInt(pageSize);
      query.setInteger(0, (curpage - 1) * size);
      query.setInteger(1, size);
      List<Event> eventList = new ArrayList();
      List list = query.list();
      if ((list != null) && (list.size() > 0)) {
        for (int i = 0; i < list.size(); i++)
        {
          Event event = new Event();
          Object[] obj = (Object[])list.get(i);
          event.setEventId(obj[0] + "");
          event.setEventTitle(obj[1] + "");
          event.setEventType(getEventTypeName(obj[2] + ""));
          event.setEventCreateDate(obj[3] + "");
          event.setContent(obj[4] + "");
          event.setEventStatus(obj[5] + "");
          
          String eventId = obj[0] + "";
          boolean flag = getFocusEventStatus(eventId, userId);
          if (flag) {
            event.setFollow("1");
          } else {
            event.setFollow("0");
          }
          eventList.add(event);
        }
      }
      vo.setEventList(eventList);
    }
    return vo;
  }
  
  private boolean getFocusEventStatus(String eventId, String userId)
  {
    Session session = getSession();
    String depIdsql = "select id from toc_follow where userid = '" + userId + "' and evnentid ='" + eventId + "'";
    SQLQuery dquery = session.createSQLQuery(depIdsql);
    List list = dquery.list();
    if ((!list.isEmpty()) && (list.size() > 0)) {
      return true;
    }
    return false;
  }
  
  public EventVo getAllEventList(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    String sql = "";
    if ("1".equals(eventStatus))
    {
      String sqlcount1 = "select count(*) from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=3";
      String sqlcount2 = "select count(*) from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=2";
      String sqlcount3 = "select count(*) from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=1";
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      SQLQuery queryCount2 = session.createSQLQuery(sqlcount2);
      SQLQuery queryCount3 = session.createSQLQuery(sqlcount3);
      int outEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
      int nearlyOutEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
      int dealingEventNum = Integer.parseInt(queryCount3.list().get(0).toString());
      vo.setEventTimeoutTotal(outEventNum);
      vo.setEventDealingTotal(dealingEventNum);
      vo.setEventOutingTotal(nearlyOutEventNum);
      if ("1".equals(subStatus)) {
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=3 order by e.isrecive desc,e.create_date DESC limit ?,?";
      } else if ("2".equals(subStatus)) {
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=2 order by e.isrecive desc,e.create_date DESC limit ?,?";
      } else if ("3".equals(subStatus)) {
        sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.status in(4,8) and e.isdelete=0  and timeout_status=1 order by e.isrecive desc,e.create_date DESC limit ?,?";
      }
    }
    else if ("2".equals(eventStatus))
    {
      String totalSql = "select count(*) from toc_event e WHERE  e.isdelete=0 and e.status in(5,6) order by e.create_date DESC";
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int solvedNum = Integer.parseInt(totalCount.list().get(0).toString());
      vo.setEventSolvedTotal(solvedNum);
      
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.isdelete=0 and e.status in(5,6) order by e.create_date DESC limit ?,?";
    }
    else if ("3".equals(eventStatus))
    {
      String totalSql = "select count(*) from toc_event e WHERE  e.isdelete=0 and e.status =7 order by e.create_date DESC";
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int closedNum = Integer.parseInt(totalCount.list().get(0).toString());
      vo.setEventSolvedTotal(closedNum);
      
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.isdelete=0 and e.status =7 order by e.create_date DESC limit ?,?";
    }
    else if ("4".equals(eventStatus))
    {
      String sqlcount1 = "select count(*) from toc_event e WHERE  e.isdelete=0 ";
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      int total = Integer.parseInt(queryCount1.list().get(0).toString());
      vo.setEventTotal(total);
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.isdelete=0  order by e.isrecive desc,e.create_date DESC limit ?,?";
    }
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventType(getEventTypeName(obj[2] + ""));
        event.setEventCreateDate(obj[3] + "");
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        
        String eventId = obj[0] + "";
        boolean flag = getFocusEventStatus(eventId, userId);
        if (flag) {
          event.setFollow("1");
        } else {
          event.setFollow("0");
        }
        eventList.add(event);
      }
    }
    vo.setEventList(eventList);
    return vo;
  }
  
  public EventVo getFocusEventList(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    String sql = "";
    if ("1".equals(eventStatus))
    {
      String sqlcount1 = "SELECT count(*) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=3 and e.isdelete=0 order by e.create_date DESC";
      
      String sqlcount2 = "SELECT count(*) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=2 and e.isdelete=0 order by e.create_date DESC";
      
      String sqlcount3 = "SELECT count(*) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=1 and e.isdelete=0 order by e.create_date DESC";
      
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      SQLQuery queryCount2 = session.createSQLQuery(sqlcount2);
      SQLQuery queryCount3 = session.createSQLQuery(sqlcount3);
      int outEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
      int nearlyOutEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
      int dealingEventNum = Integer.parseInt(queryCount3.list().get(0).toString());
      vo.setEventTimeoutTotal(outEventNum);
      vo.setEventDealingTotal(dealingEventNum);
      vo.setEventOutingTotal(nearlyOutEventNum);
      if ("1".equals(subStatus)) {
        sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=3 and e.isdelete=0 order by e.isrecive desc,e.create_date DESC limit ?,?";
      } else if ("2".equals(subStatus)) {
        sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=2 and e.isdelete=0 order by e.isrecive desc,e.create_date DESC limit ?,?";
      } else if ("3".equals(subStatus)) {
        sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in(4,8)  AND e.timeout_status=1 and e.isdelete=0 order by e.isrecive desc,e.create_date DESC limit ?,?";
      }
    }
    else if ("2".equals(eventStatus))
    {
      String totalSql = "SELECT count(*) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in (5,6) and e.isdelete=0 order by e.create_date DESC";
      
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int solvedNum = Integer.parseInt(totalCount.list().get(0).toString());
      vo.setEventSolvedTotal(solvedNum);
      
      sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status in (5,6) and e.isdelete=0 order by e.create_date DESC limit ?,?";
    }
    else if ("3".equals(eventStatus))
    {
      String totalSql = "SELECT count(*) FROM (  SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status =7 and e.isdelete=0 order by e.create_date DESC";
      
      SQLQuery totalCount = session.createSQLQuery(totalSql);
      int closedNum = Integer.parseInt(totalCount.list().get(0).toString());
      vo.setEventSolvedTotal(closedNum);
      
      sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.status =7 and e.isdelete=0 order by e.create_date DESC limit ?,?";
    }
    else if ("4".equals(eventStatus))
    {
      String sqlcount1 = "SELECT count(*) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.isdelete=0 ";
      
      SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
      int total = Integer.parseInt(queryCount1.list().get(0).toString());
      vo.setEventTotal(total);
      sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.isdelete=0 order by e.isrecive desc,e.create_date DESC limit ?,?";
    }
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventType(getEventTypeName(obj[2] + ""));
        event.setEventCreateDate(obj[3] + "");
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        event.setFollow("1");
        eventList.add(event);
      }
    }
    vo.setEventList(eventList);
    return vo;
  }
  
  public EventVo getUnDepartEventList(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    
    String count = "SELECT count(*) FROM toc_event e WHERE e.timeout_status=3  and e.isdelete=0 and e.depart='0' ";
    SQLQuery queryCount = session.createSQLQuery(count);
    int outEventNum = Integer.parseInt(queryCount.list().get(0).toString());
    vo.setEventTimeoutTotal(outEventNum);
    
    String count1 = "SELECT count(*) FROM toc_event e WHERE e.timeout_status=2 and e.isdelete=0 and e.depart= '0' ";
    SQLQuery queryCount1 = session.createSQLQuery(count1);
    int nearlyOutEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
    vo.setEventOutingTotal(nearlyOutEventNum);
    
    String count2 = "SELECT count(*) FROM toc_event e WHERE e.timeout_status=1 and e.isdelete=0 and e.depart='0' ";
    SQLQuery queryCount2 = session.createSQLQuery(count2);
    int dealingEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
    vo.setEventDealingTotal(dealingEventNum);
    if ("1".equals(subStatus))
    {
      List<Event> timeOutEvent = new ArrayList();
      String sql = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM toc_event e WHERE e.timeout_status=3 and e.isdelete=0 and e.depart='0' order by e.create_date DESC limit ?,?";
      SQLQuery query = session.createSQLQuery(sql);
      int curpage = Integer.parseInt(page);
      int size = Integer.parseInt(pageSize);
      query.setInteger(0, (curpage - 1) * size);
      query.setInteger(1, size);
      List list = query.list();
      if ((list != null) && (list.size() > 0)) {
        for (int i = 0; i < list.size(); i++)
        {
          Event event = new Event();
          Object[] obj = (Object[])list.get(i);
          event.setEventId(obj[0] + "");
          event.setEventTitle(obj[1] + "");
          event.setEventType(getEventTypeName(obj[2] + ""));
          event.setEventCreateDate(obj[3] + "");
          event.setContent(obj[4] + "");
          event.setEventStatus(obj[5] + "");
          
          String eventId = obj[0] + "";
          boolean flag = getFocusEventStatus(eventId, userId);
          if (flag) {
            event.setFollow("1");
          } else {
            event.setFollow("0");
          }
          timeOutEvent.add(event);
        }
      }
      vo.setEventList(timeOutEvent);
    }
    if ("2".equals(subStatus))
    {
      List<Event> timeNearlyOutEvent = new ArrayList();
      String sql1 = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM toc_event e WHERE e.timeout_status=2 and e.isdelete=0 and e.depart= '0' order by e.create_date DESC limit ?,?";
      SQLQuery query1 = session.createSQLQuery(sql1);
      int curpage = Integer.parseInt(page);
      int size = Integer.parseInt(pageSize);
      query1.setInteger(0, (curpage - 1) * size);
      query1.setInteger(1, size);
      List list1 = query1.list();
      if ((list1 != null) && (list1.size() > 0)) {
        for (int i = 0; i < list1.size(); i++)
        {
          Event event = new Event();
          Object[] obj = (Object[])list1.get(i);
          event.setEventId(obj[0] + "");
          event.setEventTitle(obj[1] + "");
          event.setEventType(getEventTypeName(obj[2] + ""));
          event.setEventCreateDate(obj[3] + "");
          event.setContent(obj[4] + "");
          event.setEventStatus(obj[5] + "");
          
          String eventId = obj[0] + "";
          boolean flag = getFocusEventStatus(eventId, userId);
          if (flag) {
            event.setFollow("1");
          } else {
            event.setFollow("0");
          }
          timeNearlyOutEvent.add(event);
        }
      }
      vo.setEventList(timeNearlyOutEvent);
    }
    if ("3".equals(subStatus))
    {
      List<Event> timeDealingEvent = new ArrayList();
      String sql2 = "SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM toc_event e WHERE e.timeout_status=1 and e.isdelete=0 and e.depart='0' order by e.create_date DESC limit ?,?";
      SQLQuery query2 = session.createSQLQuery(sql2);
      int curpage = Integer.parseInt(page);
      int size = Integer.parseInt(pageSize);
      query2.setInteger(0, (curpage - 1) * size);
      query2.setInteger(1, size);
      List list2 = query2.list();
      if ((list2 != null) && (list2.size() > 0)) {
        for (int i = 0; i < list2.size(); i++)
        {
          Event event = new Event();
          Object[] obj = (Object[])list2.get(i);
          event.setEventId(obj[0] + "");
          event.setEventTitle(obj[1] + "");
          event.setEventType(getEventTypeName(obj[2] + ""));
          event.setEventCreateDate(obj[3] + "");
          event.setContent(obj[4] + "");
          event.setEventStatus(obj[5] + "");
          
          String eventId = obj[0] + "";
          boolean flag = getFocusEventStatus(eventId, userId);
          if (flag) {
            event.setFollow("1");
          } else {
            event.setFollow("0");
          }
          timeDealingEvent.add(event);
        }
      }
      vo.setEventList(timeDealingEvent);
    }
    return vo;
  }
  
  public EventInfoVo getEventInfo(String eventId)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String hql = "from EventEntity where 1=1 and id= ?";
    String hql1 = "from EventDynamicsEntity where 1=1 and eVENT_ID = ? order by createDate asc";
    String hql2 = "from EventCommentEntity where 1=1 and eVNET_ID = ? order by createDate asc";
    
    List<EventEntity> eventList = findHql(hql, new Object[] { eventId });
    List<EventDynamicsEntity> dynamicsList = findHql(hql1, new Object[] { eventId });
    List<EventCommentEntity> commentList = findHql(hql2, new Object[] { eventId });
    List<String> beforePics = new ArrayList();
    List<String> afterPics = new ArrayList();
    EventInfoVo vo = new EventInfoVo();
    if ((eventList != null) && (eventList.size() > 0))
    {
      EventEntity entity = (EventEntity)eventList.get(0);
      vo.setId(entity.getId());
      vo.setTitle(entity.getTitle());
      if (StringUtils.isNotBlank(entity.getAfterPic1())) {
        afterPics.add(entity.getAfterPic1());
      }
      if (StringUtils.isNotBlank(entity.getAfterPic2())) {
        afterPics.add(entity.getAfterPic2());
      }
      if (StringUtils.isNotBlank(entity.getAfterPic3())) {
        afterPics.add(entity.getAfterPic3());
      }
      vo.setAfterPic1(entity.getAfterPic1());
      vo.setAfterPic2(entity.getAfterPic2());
      vo.setAfterPic3(entity.getAfterPic3());
      if (StringUtils.isNotBlank(entity.getArea()))
      {
        vo.setArea(getAreaName(entity.getArea()));
        vo.setAreaId(entity.getArea());
      }
      if (StringUtils.isNotBlank(entity.getBeforePic1())) {
        beforePics.add(entity.getBeforePic1());
      }
      if (StringUtils.isNotBlank(entity.getBeforePic2())) {
        beforePics.add(entity.getBeforePic2());
      }
      if (StringUtils.isNotBlank(entity.getBeforePic3())) {
        beforePics.add(entity.getBeforePic3());
      }
      vo.setBeforePic1(entity.getBeforePic1());
      vo.setBeforePic2(entity.getBeforePic2());
      vo.setBeforePic3(entity.getBeforePic3());
      if (entity.getBudgetTime() != null) {
        vo.setBudgetTime(sdf.format(entity.getBudgetTime()));
      }
      vo.setCheckStatus(entity.getCheckStatus());
      vo.setContact(entity.getContact());
      vo.setContent(entity.getContent());
      if (entity.getCreateDate() != null) {
        vo.setCreateDate(sdf.format(entity.getCreateDate()));
      }
      vo.setCreateName(entity.getCreateName());
      if (StringUtils.isNotBlank(entity.getDepart()))
      {
        vo.setDepart(getDepartName(entity.getDepart()));
        vo.setDepartId(entity.getDepart());
      }
      vo.setDetailedarea(entity.getDetailedarea());
      if (StringUtils.isNotBlank(entity.getEventType()))
      {
        vo.setEventType(getEventTypeNameInfo(entity.getEventType()));
        vo.setEventTypeId(entity.getEventType());
      }
      vo.setIsdelete(entity.getIsdelete());
      vo.setNumber(entity.getNumber());
      vo.setRecipient(entity.getRecipient());
      vo.setRemakes(entity.getRemakes());
      vo.setSource(entity.getSource());
      Integer status = entity.getStatus();
      vo.setStatusName(castStatus(status));
      vo.setStatus(status + "");
      vo.setTimeoutStatus(entity.getTimeoutStatus());
      vo.setUserid(entity.getUserid());
      vo.setAfterPics(afterPics);
      vo.setBeforePics(beforePics);
    }
    List<EventDynamics> eventDynamics = new ArrayList();
    if ((dynamicsList != null) && (dynamicsList.size() > 0)) {
      for (EventDynamicsEntity dyEntity : dynamicsList)
      {
        EventDynamics dynamic = new EventDynamics();
        dynamic.setId(dyEntity.getId());
        dynamic.setEventId(eventId);
        dynamic.setCreateName(dyEntity.getCreateName());
        if (dyEntity.getCreateDate() != null) {
          dynamic.setCreateDate(sdf.format(dyEntity.getCreateDate()));
        }
        if ((dyEntity.getSysOrgCode() != null) || (dyEntity.getSysOrgCode() != "")) {
          dynamic.setSysOrgCode(getSysOrgNameByCode(dyEntity.getSysOrgCode()));
        }
        if (StringUtils.isNotBlank(dyEntity.getDycontent()))
        {
          String content = dyEntity.getDycontent();
          String createName = dyEntity.getCreateName();
          try
          {
            if (StringUtils.isNotBlank(createName))
            {
              String replaceAll = content.replaceAll(createName, "");
              dynamic.setDycontent(replaceAll);
            }
            else
            {
              dynamic.setDycontent(content);
            }
          }
          catch (Exception e) {}
        }
        eventDynamics.add(dynamic);
      }
    }
    vo.setEventDynamics(eventDynamics);
    
    List<EventComment> eventComments = new ArrayList();
    if ((commentList != null) && (commentList.size() > 0)) {
      for (EventCommentEntity ce : commentList)
      {
        EventComment comment = new EventComment();
        comment.setId(ce.getId());
        comment.setCreateName(ce.getCreateName());
        if (ce.getCreateDate() != null) {
          comment.setCreateDate(sdf.format(ce.getCreateDate()));
        }
        comment.setEvnetId(ce.getEventId());
        comment.setSysOrgCode(getSysOrgNameByCode(ce.getSysOrgCode()));
        String createName = ce.getCreateName();
        String comment2 = ce.getComment();
        if (StringUtils.isNotBlank(createName))
        {
          String replaceAll = comment2.replaceAll(createName, "");
          comment.setComment(replaceAll);
        }
        else
        {
          comment.setComment(ce.getComment());
        }
        eventComments.add(comment);
      }
    }
    vo.setEventComments(eventComments);
    return vo;
  }
  
  private String getSysOrgNameByCode(String sysOrgCode)
  {
    String sql = "select departname from t_s_depart where org_code='" + sysOrgCode + "'";
    Session session = getSession();
    SQLQuery query2 = session.createSQLQuery(sql);
    List list2 = query2.list();
    String orgName = "";
    if ((list2 != null) && (list2.size() > 0)) {
      orgName = list2.get(0).toString();
    }
    return orgName;
  }
  
  private String getDepartName(String departId)
  {
    String sql = "select departname from t_s_depart where id='" + departId + "'";
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(sql);
    List list = query.list();
    String departname = "";
    if ((list != null) && (list.size() > 0)) {
      departname = list.get(0).toString();
    }
    return departname;
  }
  
  private String getEventTypeNameInfo(String eventTypeId)
  {
    String sql = "select name,father_id from toc_event_type where id='" + eventTypeId + "'";
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(sql);
    List list = query.list();
    String typeName = "";
    if (!list.isEmpty())
    {
      Object[] obj = (Object[])list.get(0);
      if (obj[1] != null)
      {
        String name = getEventTypeName(obj[1].toString());
        typeName = name + "-" + obj[0].toString();
      }
      else
      {
        typeName = obj[0].toString();
      }
    }
    return typeName;
  }
  
  private String getEventTypeName(String eventTypeId)
  {
    String sql = "select name from toc_event_type where id='" + eventTypeId + "'";
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(sql);
    List list = query.list();
    String typeName = "";
    if (!list.isEmpty()) {
      typeName = list.get(0).toString();
    }
    return typeName;
  }
  
  private String getAreaName(String areaId)
  {
    String sql = "select area_name from toc_event_area where id='" + areaId + "'";
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(sql);
    List list = query.list();
    String areaName = "";
    if ((list != null) && (list.size() > 0)) {
      areaName = list.get(0).toString();
    }
    return areaName;
  }
  
  private String castStatus(Integer status)
  {
    String name = "";
    switch (status.intValue())
    {
    case 1: 
      name = "待提交";
      break;
    case 2: 
      name = "待指派";
      break;
    case 3: 
      name = "待下派";
      break;
    case 4: 
      name = "处理中";
      break;
    case 5: 
      name = "部门审核中";
      break;
    case 6: 
      name = "toc审核中";
      break;
    case 7: 
      name = "已关闭";
    }
    return name;
  }
  
  public List<EventTypeVo> findEventTypeByID(String typeId)
  {
    List<EventTypeVo> typeList = new ArrayList();
    StringBuffer str = new StringBuffer();
    str.append(" select id,name,father_id from toc_event_type");
    if (StringUtils.isNotBlank(typeId)) {
      str.append(" where id = '" + typeId + "'");
    } else {
      str.append(" where father_id IS NULL");
    }
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(str.toString());
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        EventTypeVo typeVo = new EventTypeVo();
        Object[] obj = (Object[])list.get(i);
        typeVo.setId(obj[0] + "");
        typeVo.setName(obj[1] + "");
        List<EventType> types = new ArrayList();
        if ((obj[2] == null) || (obj[2] == ""))
        {
          String sql = "select id,name from toc_event_type where father_id='" + typeId + "'";
          SQLQuery query1 = session.createSQLQuery(sql);
          List list1 = query1.list();
          if ((list1 != null) && (list1.size() > 0)) {
            for (int j = 0; j < list1.size(); j++)
            {
              EventType type = new EventType();
              Object[] obj1 = (Object[])list1.get(j);
              type.setSubId(obj1[0] + "");
              type.setSubName(obj1[1] + "");
              types.add(type);
            }
          }
        }
        typeVo.setSubEventType(types);
        typeList.add(typeVo);
      }
    }
    return typeList;
  }
  
  public List<Event> searchEventByParam(Map<String, Object> map)
  {
    List<Event> eventList = new ArrayList();
    String title = map.get("title").toString();
    String content = map.get("content").toString();
    String type = map.get("type").toString();
    String subType = map.get("subType").toString();
    String area = map.get("area").toString();
    String sdate = map.get("sdate").toString();
    String edate = map.get("edate").toString();
    String page = map.get("page").toString();
    String pageSize = map.get("pageSize").toString();
    String userId = map.get("userId").toString();
    String depId = map.get("depId").toString();
    String singn = map.get("sign").toString();
    StringBuffer str = new StringBuffer();
    if ((StringUtils.isNotBlank(singn)) && ("6".equals(singn))) {
      str.append("SELECT e.id,e.title,e.event_type,e.create_date,e.content,e.status FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.isdelete='0' ");
    } else {
      str.append("select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e where e.isdelete = 0 ");
    }
    if (StringUtils.isNotBlank(title)) {
      str.append(" and e.title like '%" + title + "%' ");
    }
    if (StringUtils.isNotBlank(content)) {
      str.append(" and e.content like '%" + content + "%' ");
    }
    if ((StringUtils.isNotBlank(type)) && (StringUtils.isBlank(subType))) {
      str.append(" and e.event_type = '" + type + "' ");
    }
    if ((StringUtils.isNotBlank(type)) && (StringUtils.isNotBlank(subType))) {
      str.append(" and e.event_type = '" + subType + "' ");
    }
    if (StringUtils.isNotBlank(area)) {
      str.append(" and e.area ='" + area + "' ");
    }
    if (StringUtils.isNotBlank(sdate)) {
      str.append(" and e.create_date >= '" + sdate + "' ");
    }
    if (StringUtils.isNotBlank(edate)) {
      str.append(" and e.create_date <= '" + edate + "' ");
    }
    if ((StringUtils.isNotBlank(singn)) && ("1".equals(singn))) {
      str.append(" and e.recipient = '" + userId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("2".equals(singn))) {
      str.append(" and e.depart= '" + depId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("3".equals(singn))) {
      str.append(" and e.depart= '0' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("4".equals(singn))) {
      str.append(" and e.userid='" + userId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("5".equals(singn))) {
      str.append(" and e.status in (1,3,5) ");
    }
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    Session session = getSession();
    if ((StringUtils.isBlank(singn)) || ("1".equals(singn))) {
      str.append("order by e.isrecive desc,e.create_date desc limit ?,?");
    } else {
      str.append("order by e.isrecive desc,e.create_date desc limit ?,?");
    }
    SQLQuery query = session.createSQLQuery(str.toString());
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List list = query.list();
    if (!list.isEmpty()) {
      for (int i = 0; i < list.size(); i++)
      {
        Event ev = new Event();
        Object[] obj = (Object[])list.get(i);
        ev.setEventId(obj[0] + "");
        ev.setEventTitle(obj[1] + "");
        if (obj[2] != null) {
          ev.setEventType(getEventTypeName(obj[2] + ""));
        }
        if (StringUtils.isNotBlank(obj[3] + "")) {
          ev.setEventCreateDate(obj[3] + "");
        }
        if (StringUtils.isNotBlank(obj[4] + "")) {
          ev.setContent(obj[4] + "");
        }
        ev.setEventStatus(obj[5] + "");
        
        String eventId = obj[0] + "";
        boolean flag = getFocusEventStatus(eventId, userId);
        if (flag) {
          ev.setFollow("1");
        } else {
          ev.setFollow("0");
        }
        eventList.add(ev);
      }
    }
    return eventList;
  }
  
  public void focusEvent(Map<String, Object> map)
  {
    String eventId = map.get("eventId").toString();
    String userId = map.get("userId").toString();
    String status = map.get("status").toString();
    String sql = "";
    if ((StringUtils.isNotBlank(status)) && ("0".equals(status))) {
      sql = "delete from toc_follow where evnentid = '" + eventId + "' and userId='" + userId + "'";
    } else if ("1".equals(status)) {
      sql = "insert into toc_follow(evnentid,userid) values ('" + eventId + "','" + userId + "')";
    }
    int i;
    if (StringUtils.isNotBlank(sql))
    {
      Session session = getSession();
      SQLQuery query = session.createSQLQuery(sql);
      i = query.executeUpdate();
    }
  }
  
  public void addAppEventComment(Map<String, Object> map)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String formatTime = DateUtils.formatTime(new Date());
    if (!map.isEmpty())
    {
      String createBy = map.get("userName").toString();
      String createName = map.get("realName").toString();
      String eventId = map.get("eventId").toString();
      String comment = map.get("comment").toString();
      String createDate = formatTime;
      String uuId = UUIDGenerator.generate();
      comment = createName + comment;
      String sql = "INSERT INTO toc_event_comment (id,create_name,create_by,create_date,evnet_id,comment) VALUES ( '" + uuId + "','" + createName + "','" + createBy + "','" + createDate + "','" + eventId + "','" + comment + "')";
      
      Session session = getSession();
      SQLQuery query = session.createSQLQuery(sql);
      query.executeUpdate();
    }
  }
  
  public void addAppEventDymanics(Map<String, Object> map)
    throws ParseException
  {
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String createDate = simpleDateFormat.format(new Date());
    String time = DateUtils.getDate("MM-dd HH:mm");
    if (!map.isEmpty())
    {
      String uuId = UUIDGenerator.generate();
      String createName = map.get("userName").toString();
      String depName = map.get("depName").toString();
      String eventId = map.get("eventId").toString();
      
      String content = map.get("dycontent").toString();
      String dycontent = time + createName + content;
      String sql = "insert into toc_event_dynamics (id,create_name,create_date,dycontent,event_id) values ( '" + uuId + "','" + createName + "','" + createDate + "','" + dycontent + "','" + eventId + "')";
      
      Session session = getSession();
      SQLQuery query = session.createSQLQuery(sql);
      query.executeUpdate();
    }
  }
  
  public List<EventAreaVo> getAreaInfoById(String areaId)
  {
    List<EventAreaVo> areaInfoList = new ArrayList();
    StringBuffer str = new StringBuffer();
    str.append(" select id,area_name from toc_event_area ");
    if (StringUtils.isNotBlank(areaId)) {
      str.append(" where id = '" + areaId + "'");
    }
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(str.toString());
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        EventAreaVo vo = new EventAreaVo();
        Object[] obj = (Object[])list.get(i);
        vo.setId(obj[0] + "");
        vo.setAreaName(obj[1] + "");
        areaInfoList.add(vo);
      }
    }
    return areaInfoList;
  }
  
  public List<DepEmployeeVo> getEmployeeByDepid(String depId)
  {
    List<DepEmployeeVo> empInfoList = new ArrayList();
    StringBuffer str = new StringBuffer();
    str.append(" select user_id from t_s_user_org where org_id ='" + depId + "'");
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(str.toString());
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        String userId = list.get(i).toString();
        SQLQuery query2 = session.createSQLQuery("SELECT ts.id,ts.realname,tu.portrait FROM t_s_base_user ts left join t_s_user tu on ts.id=tu.id WHERE ts.id='" + userId + "'");
        List list2 = query2.list();
        if ((list2 != null) && (list2.size() > 0)) {
          for (int j = 0; j < list2.size(); j++)
          {
            DepEmployeeVo vo = new DepEmployeeVo();
            Object[] obj = (Object[])list2.get(j);
            vo.setId(obj[0] + "");
            vo.setUserName(obj[1] + "");
            vo.setUserImg(obj[2] + "");
            empInfoList.add(vo);
          }
        }
      }
    }
    return empInfoList;
  }
  
  public void updateEventStatus(Map<String, Object> map)
  {
    String eventId = map.get("eventId").toString();
    String status = map.get("afterStatus").toString();
    String sql = "update toc_event set status='" + status + "' where id='" + eventId + "'";
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(sql);
    query.executeUpdate();
  }
  
  public void updateEventDealTime(String eventId, String dealTime)
  {
    Session session = getSession();
    String sql = "update toc_event set process ='" + dealTime + "' where id='" + eventId + "'";
    SQLQuery query = session.createSQLQuery(sql);
    query.executeUpdate();
  }
  
  public List getAduitUser(String departId)
  {
    Session session = getSession();
    
    SQLQuery query = session.createSQLQuery("SELECT USER.id FROM t_s_base_user USER LEFT JOIN t_s_user_org org ON USER.id = org.user_id WHERE  USER.id in (SELECT tu.userid from t_s_role_user tu where tu.roleid in (SELECT tr.ID FROM t_s_role tr where tr.rolecode ='admanger')) AND org.org_id = '" + departId + "'");
    List list = query.list();
    List imeList = new ArrayList();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        String userId = list.get(i).toString();
        
        SQLQuery query1 = session.createSQLQuery("SELECT imei FROM toc_app_imei WHERE userid = '" + userId + "'");
        List lists = query1.list();
        if ((lists != null) && (lists.size() > 0))
        {
          String imei = lists.get(0).toString();
          if (StringUtils.isNotBlank(imei)) {
            imeList.add(imei);
          }
        }
      }
    }
    return imeList;
  }
  
  public List getAduitUserId()
  {
    Session session = getSession();
    
    SQLQuery query = session.createSQLQuery("SELECT USER.id FROM t_s_base_user USER WHERE  USER.id in (SELECT tu.userid from t_s_role_user tu where tu.roleid in (SELECT tr.ID FROM t_s_role tr where tr.rolecode ='admanger')) ");
    List list = query.list();
    return list;
  }
  
  public EventVo getMyAddEvent(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    String sql = "";
    
    String sqlcount1 = "select count(*) from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.check_status=6 ";
    String sqlcount2 = "select count(*) from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.check_status=5 ";
    String sqlcount3 = "select count(*) from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.status=1 ";
    SQLQuery queryCount1 = session.createSQLQuery(sqlcount1);
    SQLQuery queryCount2 = session.createSQLQuery(sqlcount2);
    SQLQuery queryCount3 = session.createSQLQuery(sqlcount3);
    int outEventNum = Integer.parseInt(queryCount1.list().get(0).toString());
    int nearlyOutEventNum = Integer.parseInt(queryCount2.list().get(0).toString());
    int dealingEventNum = Integer.parseInt(queryCount3.list().get(0).toString());
    vo.setEventTimeoutTotal(outEventNum);
    vo.setEventDealingTotal(dealingEventNum);
    vo.setEventOutingTotal(nearlyOutEventNum);
    if ("1".equals(subStatus)) {
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.status=1 order by e.create_date desc limit ?,?";
    } else if ("2".equals(subStatus)) {
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.check_status=6 order by e.create_date desc limit ?,?";
    } else if ("3".equals(subStatus)) {
      sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 and  e.check_status=5 order by e.create_date desc limit ?,?";
    }
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventType(getEventTypeName(obj[2] + ""));
        event.setEventCreateDate(obj[3] + "");
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        eventList.add(event);
      }
    }
    vo.setEventList(eventList);
    return vo;
  }
  
  public EventVo getMyAddEventNot(String userId, String eventStatus, String subStatus, String page, String pageSize)
  {
    EventVo vo = new EventVo();
    Session session = getSession();
    String sql = "select e.id,e.title,e.event_type,e.create_date,e.content,e.status from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 order by e.create_date desc limit ?,?";
    SQLQuery query = session.createSQLQuery(sql);
    int curpage = Integer.parseInt(page);
    int size = Integer.parseInt(pageSize);
    query.setInteger(0, (curpage - 1) * size);
    query.setInteger(1, size);
    List<Event> eventList = new ArrayList();
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Event event = new Event();
        Object[] obj = (Object[])list.get(i);
        event.setEventId(obj[0] + "");
        event.setEventTitle(obj[1] + "");
        event.setEventType(getEventTypeName(obj[2] + ""));
        event.setEventCreateDate(obj[3] + "");
        event.setContent(obj[4] + "");
        event.setEventStatus(obj[5] + "");
        eventList.add(event);
      }
    }
    vo.setEventList(eventList);
    
    String sql1 = "select count(*) from toc_event e WHERE  e.userid='" + userId + "' and e.isdelete=0 ";
    SQLQuery query1 = session.createSQLQuery(sql1);
    List list1 = query1.list();
    int sum = 0;
    if ((list1 != null) && (list1.size() > 0)) {
      sum = Integer.parseInt(list1.get(0).toString());
    }
    vo.setFgfTotal(sum);
    return vo;
  }
  
  public String getUserImei(String userId)
  {
    Session session = getSession();
    
    SQLQuery query1 = session.createSQLQuery("SELECT imei FROM toc_app_imei WHERE userid = '" + userId + "'");
    List lists = query1.list();
    String imei = "";
    if ((lists != null) && (lists.size() > 0)) {
      imei = lists.get(0).toString();
    }
    return imei;
  }
  
  public TocIndex getTocIndex(String userId)
  {
    TocIndex tocIndex = new TocIndex();
    Session session = getSession();
    String sql = "select * from toc_index where user_id= ? ";
    SQLQuery query = session.createSQLQuery(sql);
    query.setString(0, userId);
    List list = query.list();
    if ((list != null) && (list.size() > 0)) {
      for (int i = 0; i < list.size(); i++)
      {
        Object[] obj = (Object[])list.get(0);
        tocIndex.setId(obj[0].toString());
        tocIndex.setCreateDate(obj[1].toString());
        if (obj[2] != null) {
          tocIndex.setAduitTotal(Integer.parseInt(obj[2].toString()));
        } else {
          tocIndex.setAduitTotal(0);
        }
        if (obj[3] != null) {
          tocIndex.setMyTotal(Integer.parseInt(obj[3].toString()));
        } else {
          tocIndex.setMyTotal(0);
        }
        if (obj[4] != null) {
          tocIndex.setNodepartTotal(Integer.parseInt(obj[4].toString()));
        } else {
          tocIndex.setNodepartTotal(0);
        }
        if (obj[5] != null) {
          tocIndex.setWorkTotal(Integer.parseInt(obj[5].toString()));
        } else {
          tocIndex.setWorkTotal(0);
        }
        tocIndex.setUserId(obj[6].toString());
        if (obj[7] != null) {
          tocIndex.setFillTotal(Integer.parseInt(obj[7].toString()));
        }
      }
    }
    return tocIndex;
  }
  
  public void updateTocIndex(String userId, String uiNumber)
  {
    Session session = getSession();
    String sql1 = "select id from toc_index where user_id= ? ";
    SQLQuery query1 = session.createSQLQuery(sql1);
    query1.setString(0, userId);
    List list1 = query1.list();
    if ((list1 != null) && (list1.size() > 0))
    {
      String sql = "";
      if (uiNumber.equals("1")) {
        sql = "update toc_index set my_total=0 where user_id= ? ";
      } else if (uiNumber.equals("2")) {
        sql = "update toc_index set auit_total=0 where user_id= ? ";
      } else if (uiNumber.equals("6")) {
        sql = "update toc_index set nodepart_total=0 where user_id= ? ";
      }
      SQLQuery query = session.createSQLQuery(sql);
      query.setString(0, userId);
      query.executeUpdate();
    }
  }
  
  public void modifyTocIndex(String userId, String uiNumber, int indexNum)
  {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Session session = getSession();
    String sql1 = "select id from toc_index where user_id= ? ";
    SQLQuery query1 = session.createSQLQuery(sql1);
    query1.setString(0, userId);
    List list1 = query1.list();
    if ((list1 != null) && (list1.size() > 0))
    {
      String sql = "";
      if (uiNumber.equals("1")) {
        sql = "update toc_index set my_total=? where user_id= ? ";
      } else if (uiNumber.equals("2")) {
        sql = "update toc_index set auit_total=? where user_id= ? ";
      } else if (uiNumber.equals("6")) {
        sql = "update toc_index set fill_total=? where user_id= ? ";
      }
      SQLQuery query = session.createSQLQuery(sql);
      query.setInteger(0, indexNum);
      query.setString(1, userId);
      query.executeUpdate();
    }
    else
    {
      String sql = "";
      if (uiNumber.equals("1")) {
        sql = "insert into toc_index(create_date,my_total,user_id) value(?,?,?)";
      } else if (uiNumber.equals("2")) {
        sql = "insert into toc_index(create_date,auit_total,user_id) value(?,?,?)";
      } else if (uiNumber.equals("6")) {
        sql = "insert into toc_index(create_date,fill_total,user_id) value(?,?,?)";
      }
      SQLQuery query = session.createSQLQuery(sql);
      String date = sdf.format(new Date());
      query.setString(0, date);
      query.setInteger(1, indexNum);
      query.setString(2, userId);
      query.executeUpdate();
    }
  }
  
  public long getEventSearchSum(Map<String, Object> map)
  {
    List<Event> eventList = new ArrayList();
    String title = map.get("title").toString();
    String content = map.get("content").toString();
    String type = map.get("type").toString();
    String subType = map.get("subType").toString();
    String area = map.get("area").toString();
    String sdate = map.get("sdate").toString();
    String edate = map.get("edate").toString();
    String page = map.get("page").toString();
    String pageSize = map.get("pageSize").toString();
    String userId = map.get("userId").toString();
    String depId = map.get("depId").toString();
    String singn = map.get("sign").toString();
    StringBuffer str = new StringBuffer();
    if ((StringUtils.isNotBlank(singn)) && ("6".equals(singn))) {
      str.append("SELECT count(e.id) FROM ( SELECT * FROM toc_follow f WHERE f.userid='" + userId + "' ) k LEFT JOIN toc_event e ON k.evnentid = e.id  WHERE e.isdelete='0' ");
    } else {
      str.append("select count(e.id) from toc_event e where e.isdelete = 0 ");
    }
    if (StringUtils.isNotBlank(title)) {
      str.append(" and e.title like '%" + title + "%' ");
    }
    if (StringUtils.isNotBlank(content)) {
      str.append(" and e.content like '%" + content + "%' ");
    }
    if ((StringUtils.isNotBlank(type)) && (StringUtils.isBlank(subType))) {
      str.append(" and e.event_type = '" + type + "' ");
    }
    if ((StringUtils.isNotBlank(type)) && (StringUtils.isNotBlank(subType))) {
      str.append(" and e.event_type = '" + subType + "' ");
    }
    if (StringUtils.isNotBlank(area)) {
      str.append(" and e.area ='" + area + "' ");
    }
    if (StringUtils.isNotBlank(sdate)) {
      str.append(" and e.create_date >= '" + sdate + "' ");
    }
    if (StringUtils.isNotBlank(edate)) {
      str.append(" and e.create_date <= '" + edate + "' ");
    }
    if ((StringUtils.isNotBlank(singn)) && ("1".equals(singn))) {
      str.append(" and e.recipient = '" + userId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("2".equals(singn))) {
      str.append(" and e.depart= '" + depId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("3".equals(singn))) {
      str.append(" and e.depart= '0' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("4".equals(singn))) {
      str.append(" and e.userid='" + userId + "' ");
    } else if ((StringUtils.isNotBlank(singn)) && ("5".equals(singn))) {
      str.append(" and e.status in (1,3,5) ");
    }
    Session session = getSession();
    SQLQuery query = session.createSQLQuery(str.toString());
    List list = query.list();
    long sum = 0L;
    if (!list.isEmpty())
    {
      BigInteger a = (BigInteger)list.get(0);
      sum = a.longValue();
    }
    return sum;
  }
  
  public void insertBudgetTime(Map<String, String> map)
  {
    String userName = ((String)map.get("userName")).toString();
    String depName = ((String)map.get("depName")).toString();
    String eventId = ((String)map.get("eventId")).toString();
    String budgetTime = ((String)map.get("budgetTime")).toString();
    Session session = getSession();
    String str = "update toc_event set status = 4,isrecive = 0 ,budget_time=? where id=? ";
    SQLQuery query = session.createSQLQuery(str.toString());
    query.setString(0, budgetTime);
    query.setString(1, eventId);
    query.executeUpdate();
  }
}
